<?php session_start();
if (!isset($_SESSION['userId'])) {
	header("location:index.php");
}
include ("../class/config.php");
include ("../class/cls_story.php");
include ('../plugins/tinymce/editor.php');
$allow_ext = array('jpg', 'JPG', 'jpeg', 'JPEG', 'png', 'PNG', '');
if (isset($_POST['add'])) {
	$story_title = $_POST['story_title'];
	$story_short_des=$_POST['story_short_des'];
	$story_des = $_POST['story_des'];
	$story_status=$_POST['status'];
	$date = date("Y-m-d H:i:s");
	$story_image = $_FILES['story_image']['name'];
	$ext_array = explode(".", $story_image);
	$ext = array_pop($ext_array);
	if (in_array($ext, $allow_ext)) {
		move_uploaded_file($_FILES['story_image']['tmp_name'], '../uploads/story/' . $_FILES['story_image']['name']);
		//move files uploaded form browser option
		$objStory-> addStory($story_title,$story_short_des, $story_des, $story_status, $date, $story_image);
		$success = "Story added successfully.";
	} else {
		$success = "Invalide Image Format!";
	}
}
if (isset($_GET['deleteId'])) {
	$corsId = $_GET['deleteId'];
	$objStory-> deleteStory($corsId);
}
if (isset($_GET['editId'])) {
	$storyId = $_GET['editId'];
	if (isset($_POST['edit'])) {
		$story_title = $_POST['story_title' ];
		$story_short_des=$_POST['story_short_des'];
		$story_des= $_POST['story_des'];
		$story_status=$_POST['status'];
		$date = date("Y-m-d H:i:s");
		$story_image = $_FILES['story_image']['name'];
		$ext_array = explode(".", $story_image);
		$ext = array_pop($ext_array);
		if (in_array($ext, $allow_ext)) {
		move_uploaded_file($_FILES['story_image']['tmp_name'], '../uploads/story/' . $_FILES['story_image']['name']);
			//move files uploaded form browser option
			$objStory-> editStory($storyId,$story_title,$story_short_des, $story_des, $story_status, $date, $story_image);
			$success = "Story Edited successfully.";
		} else {
			$success = "Invalide Image Format!";
		}

	}
}
if(isset($_GET['status']))
{
	$status=$_GET['status'];
	$storyId=$_GET['storyId'];
	$objStory->storyStatus($status,$storyId);
}
?>
<script>
	var oEdit2 = new InnovaEditor("oEdit2");

		<?php	if(strpos($_SERVER["HTTP_USER_AGENT"],"MSIE")) { ?>
			oEdit2.cmdAssetManager = "modalDialogShow('../../../plugins/editor/assetmanager/assetmanager.php',800,800)";
			//Use "relative to root" path
		<?php } else { ?>
			oEdit2.cmdAssetManager = "modalDialogShow('../../../editor/assetmanager/assetmanager.php',800,800)";
			//Use "relative to root" path
		<?php } ?>
			oEdit2.btnFlash = true;
			//Show 'Insert Flash' button
			oEdit2.btnMedia = true;
			//Show 'Insert Media' button
			oEdit2.btnStyles = true;
			// show the css box
			oEdit2.height = 400;
			oEdit2.width = 500;
			oEdit2.css = './editor/assetmanager/style.css';

			oEdit2.RENDER(document.getElementById("idTemporary2").innerHTML); 
</script>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Story</title>
<script type="text/javascript" src="../lib/frmValidation.js"></script>
<link rel="stylesheet" href="../css/layout.css" />
</head>
<body>

	<div class="mainWrapper">
     <?php
		include ("../includes/adminHeader.php");
	?>
    
    
    <div class="bodyContent">
    <h1>Course</h1>
    <div class="innerBody">
    <?php if(!isset($_GET['action']))
	{?>
    <strong>Story Listing</strong>  | <a href="?action=add">Add Story</a>
     
	
      <table width="100%" border="0" cellpadding="1" cellspacing="1" class="table">
        <tr>
          <th >Sn</th>
          <th >Added Date</th>
          <th>Image</th>
          <th >Story Title</th>
          <th >Details</th>
          <th >Short Des</th>
          <th>Status</th>
          <th >Action</th>	
        </tr>
		<?php $resStory= $objStory->selectStory();
		$i=1;
		
		while($recStory=mysqli_fetch_array($resStory))
		{
		?>
        <tr>
          <td><?php echo $i++; ?></td>
          <td><?php echo $recStory['story_added_date']; ?></td>
          <td><img src="../uploads/story/<?php echo $recStory['story_image'];?>" width="50" height="50"/></td>
          <td><?php echo $recStory['story_title']; ?></td>
          <td><?php echo $recStory['story_des']; ?></td>
          <td><?php echo $recStory['story_short_des'];?></td>
          
		  <td>
		  	<?php 
          	if($recStory['story_status']=='1')
			{
          ?>
		  	<a href="?status=0&storyId=<?php echo $recStory['story_id']?>"> 
		  	<img src="../images/active.jpg" alt="Active"/></a>
		  <?php } else {
		  	?>
		  	<a href="?status=1&storyId=<?php echo $recStory['story_id']?>"> 
		  	<img src="../images/inactive.jpg" alt="Inactive"/></a>
		  	<?php }?>
		  </td>
		  
          <td><a href="?action=edit&editId=<?php echo $recStory['story_id']; ?>">Edit</a> | <a href="?deleteId=<?php echo $recStory['story_id']; ?>" onclick="javascript:return confirm('Are You Sure You Want to Delete this Record?')">Delete</a></td>
        </tr>
        <?php } /// end of while($recCategory=mysqli_fetch_array($resCategory)) ?>
       </table>
       
     <?php }
		else if(isset($_GET['action']) && $_GET['action']=='edit')
		{
		$storyId=$_GET['editId'];
		$res=$objStory->getStoryById($storyId);
		$rec=mysqli_fetch_array($res);
	 ?>
      <strong>Edit Story</strong>
      <form id="categoryEdit" name="categoryEdit" method="post" action="" enctype="multipart/form-data" onsubmit="return editCat()">
        <table width="100%" border="0" class="table" cellpadding="1" cellspacing="1">
          <tr>
            <td width="30%">Story Title *</td>
            <td><input type="text" name="story_title" id="story_title" class="textBox" value="<?php echo $rec['story_title'];?>"/></td>
          </tr>
          <tr>
            <td width="30%">Short Note *</td>
            <td><textarea rows="5" cols="50" name="story_short_des" id="story_short_des" class="textBox" /><?php echo $rec['story_short_des']?></textarea></td>
          </tr>
          <tr>
            <td width="30%">Story Details*</td>
            <td><textarea rows="5" cols="50" name="story_des" id="story_des" class="textBox" /><?php echo $rec['story_des']?></textarea></td>
          </tr>
          <tr>
          	<td width="30%">Image </td>
          	<td><input type="file" name="story_image" id="story_image"  /></td>
          </tr>
          <tr>
          	<td>Status : </td>
            <td><input type="radio" name="status" value="1" checked="checked"  />Active
            	<input type="radio" name="status" value="0" />Inactive</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td><input type="submit" name="edit" id="edit" value="Edit"  />
            	<input type="button" name="cancel" value="cancel" onclick="javascript:location.href='story.php'" /></td>
          </tr>
          <tr>
            <td>Note : * Fields are mandatory.</td>
            <td>&nbsp;</td>
          </tr>
        </table>
      </form>
 <?php }
		else if(isset($_GET['action']) && $_GET['action']=='add')
		{
	 ?>
      <strong>Add Story</strong>
      <?php
		if (isset($success)) {
			echo $success;
		}
 ?>
      <form id="addCategory" name="addCategory" method="post" action="" enctype="multipart/form-data" onsubmit="return addCat()">
        <table width="100%" border="0" class="table" cellpadding="1" cellspacing="1">
          <tr>
            <td width="30%">Story Title *</td>
            <td><input type="text" name="story_title" id="story_title" class="textBox" /></td>
          </tr>
          <tr>
            <td width="30%">Short Note *</td>
            <td><textarea rows="5" cols="50" name="story_short_des" id="story_short_des" class="textBox" /></textarea></td>
          </tr>
          <tr>
            <td width="30%">Story Details*</td>
            <td><textarea rows="5" cols="50" name="story_des" id="story_des" class="textBox" /></textarea></td>
          </tr>
          <tr>
          	<td width="30%">Image </td>
          	<td><input type="file" name="story_image" id="story_image"  /></td>
          </tr>
          <tr>
          	<td>Status : </td>
            <td><input type="radio" name="status" value="1" checked="checked"  />Active
            	<input type="radio" name="status" value="0" />Inactive</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td><input type="submit" name="add" id="add" value="Add"  />
            	<input type="button" name="cancel" value="cancel" onclick="javascript:location.href='story.php'" /></td>
          </tr>
          <tr>
            <td>Note : * Fields are mandatory.</td>
            <td>&nbsp;</td>
          </tr>
        </table>
      </form>
<?php } ?>
    </div>
    
    </div>
    
    
    <?php
		include ("../includes/adminFooter.php");
	?>
     
       </div>

</body>
</html>